package server;

import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class ServletProcess extends Thread{
    private Map<String,Servlet> servletMap = new HashMap<>();
    private Socket socket ;

    public ServletProcess(Map<String,Servlet>servletMap,Socket socket){
        this.servletMap = servletMap;
        this.socket = socket;
    }

    public Map<String, Servlet> getServletMap() {
        return servletMap;
    }

    public void setServletMap(Map<String, Servlet> servletMap) {
        this.servletMap = servletMap;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            Request request = new Request(socket.getInputStream());
            Response response = new Response(socket.getOutputStream());

            String url = request.getUrl();
            if(servletMap.containsKey(url)){
                //有方法即调用servlet
                Servlet servlet = servletMap.get(url);
                servlet.service(request,response);
            }else{
                response.outputHtml(url);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
